#ifndef GAMEPLAY_H
#define GAMEPLAY_H

#include "obstacle.h"
#include "qgraphicsitem.h"
#include "qgraphicsscene.h"
#include "qgraphicsview.h"
#include "qprogressbar.h"
#include "qpushbutton.h"
#include "qtimer.h"
#include"character.h"
#include <QWidget>
#include<QKeyEvent>
#include<QList>
#include<QMessageBox>

class gamePlay:public QWidget
{
public:
    gamePlay(QWidget *parent = nullptr);

    void message(){
        this->close();
    }
public slots:

    void chtom(){
        temp.setPixmap(QPixmap("tomfly"));
        temp.setPos(100,100);
        speed=1;
    }
    void chjerry(){
        temp.setPixmap(QPixmap("jerryfly"));
        temp.setPos(100,100);
        speed=1;
    }

    void bgmove(){
        background1.moveBy(-speed,0);
        background2.moveBy(-speed,0);
        if(background1.x()<=-500){
            background1.setX(500);
        }
        if(background2.x()<=-500){
            background2.setX(500);
        }
    }
    void treemove(){
        tree.moveBy(-speed,0);
        num=rand()%25;
        if(tree.x()<-110){
            tree.setX(500+num*20);
        }
    }

    void yestarmove(){
        yestar.moveBy(-speed,0);
        xn=rand()%24,yn=rand()%13;
        if(touchingyestar()||yestar.x()<-50){
            if(touchingyestar()){
                score+=speed*100;
                if(temp.life<800)
                    temp.life+=200;
                else temp.life=1000;
                clife.setValue(temp.life);
            }
            yestar.setPos(500+xn*20,(yn+1)*20);
        }
    }
    void redstarmove(){
        redstar.moveBy(-speed,0);
        xn=rand()%24,yn=rand()%13;
        if(touchingredstar()||redstar.x()<-50){
            if(touchingredstar()){
                score+=speed*100;
                if(temp.life>200)
                    temp.life-=200;
                else temp.life=0;
                clife.setValue(temp.life);
            }
            redstar.setPos(500+xn*20,(yn+1)*20);
        }
    }

    //增加分数
    void increasescore(){
        score+=speed;
        sscore.setText("得分："+QString::number(score));
    }
    // 槽函数，增加速度
    void increaseSpeed() {
        speed += 1; // 每次增加 1
        if (speed > 20) {
            speed = 20; // 最大速度限制为 20
        }
    }
    bool touchingyestar(){
        if(temp.x()+100>yestar.x()&&temp.y()+61>yestar.y()&&temp.x()<yestar.x()&&temp.y()<yestar.y())
            return true;
        else return false;
    }
    bool touchingredstar(){
        if(temp.x()+100>redstar.x()&&temp.y()+61>redstar.y()&&temp.x()<redstar.x()&&temp.y()<redstar.y())
            return true;
        else return false;
    }

    void touchingtree(){
        bool a=false;
        if(temp.x()+100>tree.x()&&temp.y()+61>tree.y()&&temp.x()<tree.x())
            a=true;
        if(a){
            qDebug()<<"touching the tree!";
            temp.life-=speed*5;
            clife.setValue(temp.life);
        }
    }
    void decrease(){
        temp.life-=1*speed;
        if(temp.life<0)
            temp.life=0;
        clife.setValue(temp.life);
    }

    void gameover(){
        if(temp.life<=0){
            const int ss=score;
            QMessageBox::information(this,"提示","游戏结束,得分为："+QString::number(ss));
            temp.life=1000;
            this->close();
        }
    }

public:
    QGraphicsScene  gamescene ;
    QGraphicsView  gameview;

    QGraphicsPixmapItem background1,background2;
    QGraphicsPixmapItem tom,jerry;
    character temp;

    QPushButton sscore;

    QPushButton stop;

    QProgressBar clife;

    int speed = 1; // 初始速度为 1


    //树初位
    int num;

    //星星初位
    int xn,yn;
    int xr,yr;

    QTimer dlife;


    QTimer speedTimer;

    QTimer cover;
    //背景移动
    QTimer bgm;

    //角色移动
    QTimer cmove;

    //分数计时
    QTimer cscore;

    //树移动
    QTimer mtree;

    //星星移动
    QTimer myestar;
    QTimer mredstar;

    //碰到树
    QTimer touchtree;

    //角色移动
    void charactermove();

    //分数
    int score=0;

    //障碍
    obstacle tree,yestar,redstar;

    //按键组合

    QList<int> mykeylist;

    //按键事件
    void keyPressEvent(QKeyEvent *event);

    void keyReleaseEvent(QKeyEvent *event);
};

#endif // GAMEPLAY_H
